var appInstance = getApp();
const chooseLocation = requirePlugin('chooseLocation');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    region:[],

    id:0,
    address:{
      id: "",
      customer_id: "",

      name: "",
      telephone: "",

      area_id: "",
      area_name: "",
      city_id: "",
      city_name: "",
      province_id: "",
      province_name: "",

      detail_address: "",

      is_default: 0,
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var t = this;
    if( options.id ){

      this.setData({

        id : options.id

      },function(){
        t.get_address( );
      });

    }


    // this.init();

  },

  get_address:function( ){

    var t = this;
    var url = appInstance.globalData.api_path+'&r=address.single';

    var data = {
      id : this.data.id
    };

    appInstance.request(url, data, 'GET', res=>{


      this.data.region[0] = res.data.province_name;
      this.data.region[1] = res.data.city_name;
      this.data.region[2] = res.data.area_name;

      t.setData({
        region:this.data.region,
        address:res.data
      });

    });
  },



  save:function(){

    var url = appInstance.globalData.api_path+'&r=address.edit';

    var data = this.data.address;




    appInstance.request( url, data, "POST", res => {


      if( res.data.status == 0 ){

        // 跳转到上个页面
        wx.navigateBack();

      }

    });

  },

  remove:function(){

    if( this.data.id == 0 ){
      wx.navigateBack();
      return;
    }

    var url = appInstance.globalData.api_path+'&r=address.remove';

    var data = {
      address_id:this.data.id
    };

    appInstance.request( url, data, "POST", res => {

      if( res.data.status == 0 ){
        wx.navigateBack();
      }


    });

  },

  setName:function( e ){


    this.data.address.name = e.detail.value;
    this.setData({
      address : this.data.address
    });

  },

  setTelephone:function( e ){
    this.data.address.telephone = e.detail.value;
    this.setData({
      address : this.data.address
    });
  },

  setRegion:function( e ){

    this.data.address.province_name = e.detail.value[0];
    this.data.address.city_name = e.detail.value[1];
    this.data.address.area_name = e.detail.value[2];

    this.data.address.province_id = e.detail.code[0];
    this.data.address.city_id = e.detail.code[1];
    this.data.address.area_id = e.detail.code[2];

    this.setData({

      address:this.data.address
    });


  },

  setDetailAddress:function( e ){
    this.data.address.detail_address = e.detail.value;
    this.setData({
      address : this.data.address
    });
  },

  setLocation:function(){

    var t = this;

    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy;



        t.locationPlugin( res );
      }
    });



  },

  getLocationSIG:function( params, callback ){

    var url = appInstance.globalData.api_path+'&r=address.getLocationSIG';

    var data = params;
    appInstance.request(url, data, 'GET', function( res ){

      if( !res.status ){
        callback( res.data.sig );
      }


    });

  },

  locationPlugin:function( res ){

    const key = appInstance.globalData.location_key; //使用在腾讯位置服务申请的key
    // const key = '254BZ-QQUKX-H474D-7KR67-P36L7-B6FQA'; //使用在腾讯位置服务申请的key
    const referer = '商务沟通工具'; //调用插件的app的名称
    const location = JSON.stringify({
      latitude: res.latitude,
      longitude: res.longitude
    });
    // const category = '生活服务,娱乐休闲';
    const category = '';

/*    var params = {
      path:'ws/geocoder/v1',
      key:key,
      referer:referer,
      location:location,
      category:category,
    };
    this.getLocationSIG( params,function( sig ){
      wx.navigateTo({
        url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
      });
    } );*/

    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {



  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null

    if( location ){
      this.data.address.province_name = location.province;
      this.data.address.city_name = location.city;
      this.data.address.area_name = location.district;
      this.data.address.detail_address = location.name;
      this.setData({
        address:this.data.address
      });
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  bindRegionChange:function( params ){

    this.setData({
      'region':params.detail.value
    })
  }

})